package com.Alan.util;

import org.junit.Test;
import sun.nio.cs.ISO_8859_2;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class UtilsAlan {

    public final static String ISO_8859_1 = "ISO-8859-1";
    public final static String UTF_8 = "UTF-8";

    /*
    * 字符串转为Util包下的Date
    * */
    public static java.util.Date strToUtilDate(String dateStr, boolean flag){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (!flag){
            format = new SimpleDateFormat("yyyy-MM-dd");
        }
        java.util.Date UtilDate = null;
        try {
            UtilDate = format.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("UtilsAlan -> strToSqlDate");
        }
        return UtilDate;
    }

    /*
     * 字符串转为sql包下的Date
     * */
    public static java.sql.Date strToSqlDate(String dateStr, boolean flag){
        return new java.sql.Date(strToUtilDate(dateStr, flag).getTime());
    }

    /*
     * 给定日期计算年龄
     * */
    public static HashMap<String, Integer> getAge(Date birthday){
        Calendar now = Calendar.getInstance();
        Calendar b = Calendar.getInstance();
        b.setTime(birthday);

        int year = now.get(Calendar.YEAR) - b.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH) - b.get(Calendar.MONTH);
        int day = now.get(Calendar.DAY_OF_MONTH) - b.get(Calendar.DAY_OF_MONTH);

        if (month < 0) {
            month = 12 - b.get(Calendar.MONTH) + now.get(Calendar.MONTH);
            year -= 1;
        }

        if (day < 0) {
            day = b.getMaximum(Calendar.DAY_OF_MONTH) - b.get(Calendar.DAY_OF_MONTH) + now.get(Calendar.DAY_OF_MONTH);
            month -= 1;
        }

        HashMap<String, Integer> time = new HashMap<String, Integer>();
        time.put("year", year);
        time.put("month", month);
        time.put("day", day);
        return time;
    }

    /*
     * 获取当前的时间字符串
     * */
    public static String getNowDateStr(){
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateformat.format(System.currentTimeMillis());
    }

    public static String getNowWeek(){
        return new SimpleDateFormat("E").format(System.currentTimeMillis());
    }

    /*
    * 解决requests.getParameter()乱码
    * */
    public static String transCoding(String str, String fromCharset, String goCharset){
        String transStr = "";
        try {
            transStr = new String(str.getBytes(fromCharset), goCharset);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            System.out.println("UtilsAlan -> transCoding不支持的编码格式");
        }

        return transStr;
    }

    @Test
    public void text(){
        /*String dateStr = "2019-12-04 13:14:58";
        Date date = strToSqlDate(dateStr);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        System.out.println(format);

        HashMap<String, Integer> age = UtilsAlan.getAge(strToUtilDate(dateStr));
        Integer year = age.get("year");
        System.out.println(age.get("year"));
        System.out.println(age.get("month"));
        System.out.println(age.get("day"));*/

        /*SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.sql.Date date = strToSqlDate("2021-09-05 00:05:08", true);
        String format1 = dateformat.format(date);
        System.out.println(format1);*/

        System.out.println(getNowWeek());
    }
}
